package com.rrd.itext.utils;


import java.util.ArrayList;
import java.util.List;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Document;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.Rectangle;
import com.itextpdf.text.Font.FontStyle;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPageEventHelper;
import com.itextpdf.text.pdf.PdfWriter;
import com.rrd.itext.xmlBean.layoutBean.XmlBaseBean;
import com.rrd.itext.xmlBean.layoutBean.XmlPage;
import com.rrd.itext.xmlBean.layoutBean.XmlSection;

public class HeaderFooter extends PdfPageEventHelper {
	
	private Font font;
	//Define the page number style for pure color page temporarily
	private Font whiteFont;
	private PDFBuilder builder;
    
    private Phrase[] header = new Phrase[2];
    private int pageCount;
    private int currentPageNumber;
    private boolean displayPageNumber;
    private String pageNumberPosition;
    private XmlPage currentPage;
	
	public HeaderFooter(Font font, PDFBuilder builder){
		this.font = font;
		this.builder = builder;
		try{
	        Font f = new Font(BaseFont.createFont());
	        f.setColor(new BaseColor(220, 220, 220));
	        f.setSize(13);
	        f.setStyle(FontStyle.BOLD.getValue());
	        this.whiteFont = f;
		}catch(Exception e){
		    e.printStackTrace();
            this.whiteFont = font;
		}
	}

    public void onOpenDocument(PdfWriter writer, Document document) {
        header[0] = new Phrase();
        pageCount = 0;
    }

    public void onChapter(PdfWriter writer, Document document,
            float paragraphPosition, Paragraph title) {
        header[1] = new Phrase(title.getContent());
        currentPageNumber = 0;
    }

    public void onStartPage(PdfWriter writer, Document document) {
        currentPageNumber++;
    	pageCount++;
    	this.builder.setPageCount(pageCount);
    }

    public void onEndPage(PdfWriter writer, Document document) {
        this.currentPage = this.builder.getCurrentPage();
        Boolean displayPageNumber = currentPage.isDisplayPageNumber();
        //displayPageNumber == false, ignore the current page number
        if(displayPageNumber != null && !displayPageNumber){
            if(this.displayPageNumber){
                if("left".equalsIgnoreCase(this.pageNumberPosition)){
                    pageNumberPosition="right";
                }else if("right".equalsIgnoreCase(this.pageNumberPosition)){
                    pageNumberPosition="left";
                }
            }
            return;
        }
        //First page which defined the page number displaying
        if(displayPageNumber != null && displayPageNumber && !this.displayPageNumber){
            currentPageNumber = 1;
            this.displayPageNumber = true;
            this.pageNumberPosition = this.currentPage.getPageNumberPosition();
        }
        if(!this.displayPageNumber){
            return;
        }
        Font f = null;
        if(currentPage.getZones() != null && currentPage.getZones().size() == 1 &&currentPage.getZones().get(0).getRectangle() != null){
           f = this.whiteFont;
        }
        if(f == null){
            f = this.font;
        }
        Rectangle rect = writer.getBoxSize("page");
        if("left".equalsIgnoreCase(this.pageNumberPosition)){
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_LEFT, new Phrase(Integer.toString(currentPageNumber),f), rect.getLeft(), rect.getBottom(), 0);
            pageNumberPosition="right";
        }else if("right".equalsIgnoreCase(this.pageNumberPosition)){
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_RIGHT, new Phrase(Integer.toString(currentPageNumber),f), rect.getRight(), rect.getBottom(), 0);
            pageNumberPosition="left";
        }else if("middle".equalsIgnoreCase(this.pageNumberPosition)){
            ColumnText.showTextAligned(writer.getDirectContent(), Element.ALIGN_MIDDLE, new Phrase(Integer.toString(currentPageNumber),f), rect.getLeft(), rect.getBottom(), 0);
        }
//            switch(writer.getPageNumber() % 2) {
//            case 1:
//                ColumnText.showTextAligned(writer.getDirectContent(),
//                        Element.ALIGN_RIGHT, new Phrase(Integer.toString(currentPageNumber),f),
//                        rect.getRight(), rect.getBottom(), 0);
//                break;
//            case 0:
//                ColumnText.showTextAligned(writer.getDirectContent(),
//                        Element.ALIGN_LEFT, new Phrase(Integer.toString(currentPageNumber),f),
//                        rect.getLeft(), rect.getBottom(), 0);
//                break;
//            }
    }
}
